import Cookies from 'js-cookie'

export const addCookie = (name, value, expiresHours) => {
  if (expiresHours) {
    Cookies.set(name, value, { expires: expiresHours });

  } else {
    Cookies.set(name, value);

  }
}

export const getCookie = name => {
  return Cookies.get(name);
}


export const deleteCookie = name => {
  Cookies.remove(name);
}


export const flushCookie = () => {
  var neededAttributes = {

  };
  Object.keys(Cookies.get()).forEach(function(cookieName) {
    Cookies.remove(cookieName, neededAttributes);
  });
}

export const deepCopyObj = (oldObj) => {
    if ( !oldObj || Object.prototype.toString.call(oldObj) != '[object Object]' ) return oldObj;
    return JSON.parse(JSON.stringify(oldObj));
}

export const checkPhone = (phone) => {
  if (!(/^1[34578]\d{9}$/.test(phone))) {
    return false;
  } else {
    return true
  }
}
export const checkCode = (code) => {
  if (!(/^\d{6}$/.test(code))) {
    return false;
  } else {
    return true
  }
}
export const checkZhName = (name) => {
  if (!(/^[\u4E00-\u9FA5]{2,4}$/.test(name))) {
    return false;
  } else {
    return true
  }
}

export const checkBankNum = (num) => {
  if (!(/\d{15,30}$/.test(num))) {
    return false;
  } else {
    return true
  }
}


export const checkpw = (str) => {
 var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/;
  return reg.test(str.trim());
}


export const getFormatTime = (data, detail) => {
  if (!data) {
    return "";
  }

  var date = new Date(data);
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  month = month < 10 ? ("0" + month) : month;
  var day = date.getDate();
  day = day < 10 ? ("0" + day) : day;
  var ymd = year + "-" + month + "-" + day;

  var hour = date.getHours();
  hour = hour < 10 ? ("0" + hour) : hour;
  var minu = date.getMinutes();
  minu = minu < 10 ? ("0" + minu) : minu;
  var second = date.getSeconds();
  second = second < 10 ? ("0" + second) : second;

  var xqValue = date.getDay();
  var xq = '';
  if (xqValue == 0) {
    xq = "周日";
  } else if (xqValue == 1) {
    xq = "周一";
  } else if (xqValue == 2) {
    xq = "周二";
  } else if (xqValue == 3) {
    xq = "周三";
  } else if (xqValue == 4) {
    xq = "周四";
  } else if (xqValue == 5) {
    xq = "周五";
  } else if (xqValue == 6) {
    xq = "周六";
  }
  if (detail == "md") {
    return month + "月" + day + "日";
  }
  if (detail == "m-d") {
    return month + "-" + day;
  }
  if (detail == "xq") {
    return xq;
  }
  if (detail == "hms") {
    return hour + ":" + minu + ":" + second;
  }
  if (detail == "hm") {
    return hour + ":" + minu;
  }
  if (detail == "mdhm") {
    return month + "-" + day + " " + hour + ":" + minu;
  }
  if (detail == "ymdhms") {
    return ymd + " " + hour + ":" + minu + ":" + second;
  }
  if (detail == "ymd") {
    return ymd;
  }
  if (detail == "ymdhm") {
    return year + '/' + month + '/' + day + ' ' + hour + ':' + minu;
  }
  if (detail == "dateStr") {
    return year + '' + month + day;
  }
  if (detail == "dateStr") {
    return year + '' + month + day;
  }
  if (detail == "ymd/") {
    return month + '/' + day + '/' + year;
  }
  if (detail == "ymd/hm") {
    return month + '/' + day + '/' + year + ' ' + hour + ':' + minu;
  }
  if (detail == "md/hm") {
    return month + '/' + day + ' ' + hour + ':' + minu;
  }
  if (detail == "y/m/d") {
    return year + '/' + month + '/' + day;
  }
  if (detail == "y-m-d") {
    return year + '-' + month + '-' + day;
  }
  if (detail == "m/d") {
    return month + '/' + day;
  }
  if (detail == "y/m/d/hm") {
    return year + '/' + month + '/' + day + ' ' + hour + ":" + minu;
  }
  if (detail == "y.m") {
    return year + '.' + month;
  }
}


export function gethashcode() {
  function randomWord(randomFlag, min, max) {
    var str = "",
      range = min,
      arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    // 随机产生
    if (randomFlag) {
      range = Math.round(Math.random() * (max - min)) + min;
    }
    for (var i = 0; i < range; i++) {
      var pos = Math.round(Math.random() * (arr.length - 1));
      str += arr[pos];
    }
    return str;
  }

  function hashCode(str) {
    var h = 0;
    var len = str.length;
    var t = 2147483648;
    for (var i = 0; i < len; i++) {
      h = 31 * h + str.charCodeAt(i);
      if (h > 2147483647) h %= t; //java int溢出则取模
    }
    /*var t = -2147483648 * 2;
     while (h > 2147483647) {
     h += t
     }*/
    return h;
  }
  var timestamp = (new Date()).valueOf();
  var myRandom = randomWord(false, 6);
  var hashcode = hashCode(myRandom + timestamp.toString());
  return hashcode;
}


export const deepClone = (source) => {
  if (!source && typeof source !== 'object') {
      throw new Error('error arguments', 'shallowClone');
  }
  const targetObj = source.constructor === Array ? [] : {};
  for (const keys in source) {
      if (source.hasOwnProperty(keys)) {
          if (source[keys] && typeof source[keys] === 'object') {
              targetObj[keys] = source[keys].constructor === Array ? [] : {};
              targetObj[keys] = deepClone(source[keys]);
          } else {
              targetObj[keys] = source[keys];
          }
      }
  }
  return targetObj;
}

